﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crypt;

namespace MediaManager
{
    public partial class LoginFrm : Form
    {
        public LoginFrm()
        {
            InitializeComponent();
        }

        private void LoginFrm_Load(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtPassword.Text = "";
        }
        public SendLoginSuc onSendLoginSuc = null;

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Length < 1)
                {
                    GlobalBase.Global.Instance.ShowMessage("请输入用户名！");
                    return;
                }
                if (txtPassword.Text.Length < 1)
                {
                    GlobalBase.Global.Instance.ShowMessage("请输入密码！");
                    return;
                }
                string pass = Cryption.Instance.EncryptDES(txtPassword.Text);
                Login login = _dataContext.Login.Where(p => p.name == txtName.Text && p.password == pass).Single<Login>();
                if (null != login && login.id > 0)
                {
                    if (null != onSendLoginSuc)
                    {
                        onSendLoginSuc(txtName.Text);
                        string s = string.Format(StructDayInfo.登陆, DateTime.Now);
                        InserDayInfo(s);
                        btnCancel_Click(sender, e);
                    }
                    else
                    {
                        throw new Exception("挂接不正确！");
                    }
                }
                else
                {
                    GlobalBase.Global.Instance.ShowMessage("输入的用户名或密码不正确！");
                }
            }
            catch (Exception ex)
            {
                GlobalBase.CatchException.DealWithException(ex);
                GlobalBase.Global.Instance.ShowMessage("输入的用户名或密码不正确！");
            }
        }
        private DataClasses2DataContext _dataContext = new DataClasses2DataContext();

        private void txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOk_Click(sender, e);
            }
        }
        private void InserDayInfo(string dInfo)
        {

            DayInfo dayInfo = new DayInfo();
            dayInfo.id = _dataContext.GetDayInfoIndex();
            dayInfo.name = txtName.Text;
            dayInfo.操作 = dInfo;
            dayInfo.日期 = DateTime.Now;
            _dataContext.DayInfo.InsertOnSubmit(dayInfo);
            _dataContext.SubmitChanges();

        }
    }

    public delegate void SendLoginSuc(string userName);
}
